﻿using System;
using System.Linq;
using System.Web.Mvc;
using DTTB.Common;
using DTTB.DTO;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.CommonDelegate;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.PhanQuyen;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Controllers.Base;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.ProfileModel;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.Common;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.Controllers
{
    public class ProfileController : BaseController
    {
        UsersServiceDelegate UsersServiceDelegate = new UsersServiceDelegate();
        CommonDelegate CommonDelegate = new CommonDelegate();
        DBCommonDataContext DBCommon = new DBCommonDataContext();

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ChangePassword()
        {
            //lấy username, password user hiện hành đang login
            //var userLogin = new string[] { Session["USERNAME"].ToString(), Session["PASSWORD"].ToString() SessionDetail.GetPASSWORD() };
            var userLogin = new string[] { Session["USERNAME"].ToString(), Session["PASSWORD"].ToString() };
            ViewData["userLogin"] = userLogin;
            ViewData["ChangePasswordStatus"] = "new";

            return View("ChangePassword");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ChangePassword(ChangePassForm form)
        {
            var newpassword = EncodePasswordUser.EncodePassword(form.newpassword);

            UsersDto usersDto = new UsersDto();

            try
            {
                usersDto = UsersServiceDelegate.GetUsersByUserNamePassword(form.username, form.password);
                usersDto.Password = newpassword;

                if (UsersServiceDelegate.Update(usersDto))
                {
                    ViewData["ChangePasswordStatus"] = "true";
                }
                else
                {
                    ViewData["ChangePasswordStatus"] = "false";
                }

                //Cập nhật password mới cho Session
                Session["PASSWORD"] = newpassword;

                //lấy username, password user mới thay đổi password
                var userLogin = new string[] { Session["USERNAME"].ToString(), Session["PASSWORD"].ToString() };
                ViewData["userLogin"] = userLogin;
            }
            catch (Exception)
            {
                return RedirectToAction("ChangePassword");
            }

            return View("ChangePassword");
        }

        public ActionResult PersionalInformation()
        {
            PersionalInformationForm form = new PersionalInformationForm();
            TB_User userLogin = new TB_User();
            try
            {
                userLogin = CommonDelegate.GetUserByUserId(Convert.ToString(Convert.ToInt32(Session["USERID"])));
            }
            catch (Exception)
            {
                return RedirectToAction("Logout", "Authentication");
            }

            //Khởi tạo dữ liệu form
            form.HoTen = userLogin.FullName;
            
            try
            {
                //string ngaySinh = Convert.ToString(userLogin.BirthDay) ;
                string ngaySinh = string.Format("{0:MM/dd/yyyy}", userLogin.BirthDay);
                form.NgaySinh = ngaySinh == "01/01/0001" ? string.Empty : Convert.ToString(DateTimeExtension.ConvertStringMMDDYYYYToStringDDMMYYYY(ngaySinh));
            }
            catch (Exception)
            {
                form.NgaySinh = string.Empty;
            }

            form.GioiTinh = userLogin.Sex;
            form.Email = userLogin.Email;
            form.DienThoai = userLogin.Mobile;

            return View("~/Views/Profile/PersonalInformation.aspx", form);
        }

        public ActionResult UpdatePersionalInformationAjax(string hoten, string dienthoai, string ngaysinh, string email, string gioitinh)
        {
            string[] data = new string[2];
            TB_User userLogin = new TB_User();
            int idUserLogin = Convert.ToInt32(Session["USERID"]);

            if (idUserLogin == 0)
            {
                RedirectToAction("Logout", "Authentication");
            }
            else
            {
                try
                {
                    userLogin = DBCommon.TB_Users.SingleOrDefault(p => p.ID == idUserLogin);
                    userLogin.FullName = hoten;
                    userLogin.Mobile = dienthoai;
                    try
                    {
                        userLogin.BirthDay = DateTimeExtension.ConvertStringDDMMYYYYToMMDDYYYY(ngaysinh);
                        data[1] = string.Format("{0:dd/MM/yyy}", userLogin.BirthDay);
                    }
                    catch (Exception)
                    {
                        userLogin.BirthDay = DateTime.MinValue;
                        data[1] = string.Empty;
                    }

                    userLogin.Email = email;

                    userLogin.Sex = gioitinh == "true" ? true : false;

                    DBCommon.SubmitChanges();
                    data[0] = "true";

                }
                catch (Exception)
                {
                    data[0] = "false";
                }
            }
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}
